<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  name: 'PieChart',
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    params: {
      type: Object
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        title: {
          text: this.params.title,
          left: 'center',
          top: 0,
          textStyle: {
            color: 'black'
          }
        },
        tooltip: {
          trigger: 'item',
          formatter: ' {b} : {c} ({d}%)'
          // formatter: function(param) {
          //   console.log(param.color)
          //   console.log(param.data)
          //   return [
          //     param.data.name + '<br/>' + '<hr size=1 style="margin: 3px 0">' +
          //     '<h1 style="font-size:14px;color:white;margin:0 25px 0 0">' + param.data.value + '</h1>'
          //   ].join('')
          // }
        },
        legend: {
          left: 'center',
          bottom: '0',
          data: ['达标人数', '未达标人数']
        },
        color: ['rgba(24, 144, 255, 1)', 'rgba(247, 181, 0, 1)'],
        series: [
          {
            type: 'pie',
            roseType: 'radius',
            radius: [55, 95],
            center: ['50%', '50%'],
            data: [
              { value: this.params.Count, name: '达标人数' },
              { value: this.params.userCount - this.params.Count, name: '未达标人数' }
            ],
            animationEasing: 'cubicInOut',
            animationDuration: 2600,
            label: {
              formatter: ' {per|{d}%}  ',
              backgroundColor: '#eee',
              borderColor: '#aaa',
              borderWidth: 1,
              borderRadius: 4,
              // shadowBlur:3,
              // shadowOffsetX: 2,
              // shadowOffsetY: 2,
              // shadowColor: '#999',
              // padding: [0, 7],
              rich: {
                a: {
                  color: '#999',
                  lineHeight: 22,
                  align: 'center'
                },
                // abg: {
                //     backgroundColor: '#333',
                //     width: '100%',
                //     align: 'right',
                //     height: 22,
                //     borderRadius: [4, 4, 0, 0]
                // },
                hr: {
                  borderColor: '#aaa',
                  width: '100%',
                  borderWidth: 0.5,
                  height: 0
                },
                b: {
                  fontSize: 16,
                  lineHeight: 33
                },
                per: {
                  color: '#eee',
                  backgroundColor: '#334455',
                  padding: [2, 4],
                  borderRadius: 2
                }
              }
            }
          }
        ]
      })
    }
  }
}
</script>
